package com.ckzp.jfinal.weixin.controller;

import com.ckzp.core.util.XmlUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.system.util.SysWorkLogUtil;
import com.ckzp.weixin.service.*;
import com.ckzp.weixin.service.aes.WXBizMsgCrypt;
import com.jfinal.aop.Clear;
import com.jfinal.kit.StrKit;

/**
 * Created by 51594 on 2023/4/3.
 */
public class WxApiController extends BaseController {

    @Clear
    public void index() {
        renderText("默认");
        renderText("默认2");
    }

    /**
     * 微信交互接口
     * String signature, String timestamp, String nonce
     */
    @Clear
    public void WxServiceApi() {
        String echostr = getPara("echostr");
        String signature = getPara("signature");
        String timestamp = getPara("timestamp");
        String nonce = getPara("nonce");
        String rawData = getRawData();
        String getQueryString = getRequest().getQueryString();
        if (null == echostr || echostr.isEmpty()) {
            if (ServiceTool.checkSignature(signature, timestamp, nonce)) {
                SysWorkLogUtil.n().saveLog("1", "getQueryString", getQueryString);
                SysWorkLogUtil.n().saveLog("1", "rawData", rawData);
                ServiceRequest req = (ServiceRequest) XmlUtil.toBean(rawData, ServiceRequest.class);
                if (!StrKit.notNull(req.getFromUserName())) {
                    rawData = WXBizMsgCrypt.n().decrypt(req.getEncrypt());
                    req = (ServiceRequest) XmlUtil.toBean(rawData, ServiceRequest.class);
                    SysWorkLogUtil.n().saveLog("1", "EncodeReq", req.toJson());
                }
                if (req != null) {
                    String actionClass = MessageActionCache.getActionClass(ServiceTool.getMsgTypeKey(req));
                    try {
                        if (StrKit.notBlank(actionClass)) {
                            Object cls = Class.forName(actionClass).newInstance();
                            if (cls instanceof MessageAction) {
                                ServiceResponse res = ((MessageAction) cls).doAction(req);
                                if (res != null) {
                                    SysWorkLogUtil.n().saveLog("1", "response", res.toXml());
                                    renderText(res.toXml());
                                    return;
                                }
                            } else {
                                renderText(new MessageNoneAction().doAction(req).toXml());
                                return;
                            }
                        } else {
                            SysWorkLogUtil.n().saveLog("1", "noaction", req.toJson());
                            renderText("");
                            return;
                        }
                    } catch (Exception e) {
                        SysWorkLogUtil.n().saveLog("10001", "WxError", "ERROR=" + e.toString());
                        renderText("");
                        return;
                    }
                }
            } else {
                SysWorkLogUtil.n().saveLog("10001", "WxService", "签名失败：query=" + getQueryString + "------------------rawData==" + rawData);
            }
        } else {
            renderText(echostr);
            return;
        }
        renderText("");
    }
}
